package com.twu.biblioteca;

import java.util.TreeMap;

/**
 * Created with IntelliJ IDEA.
 * User: dbelsey
 * Date: 01/02/13
 * Time: 10:24
 */
public class UserList {

    private TreeMap <String, UserData> userList;

    public UserList(){
        userList = new TreeMap <String, UserData>();
    }

    public void populateUserList(){
        try{
            add("000-0001", new UserData("User Name One","pass1", "ename@emserver.com", "09876271834"));
            add("181-7890", new UserData("User Name 2", "2word", "alpha@omega.com", "09876271834"));
            add("786-9056", new UserData("User Name Three", "paSSword", "night@day.com", "09876271834"));
        }
        catch (InvalidUserDataException e){

        }
        catch(InvalidUsernameException e){

        }
    }

    public void add(String newUserName, UserData newUserData) throws InvalidUsernameException{
        // TODO: check if user already exists - add new is not a current requirement so skip for now but should really be here
        userList.put(validateUserName(newUserName), newUserData);
    }

    private String validateUserName(String userName) throws InvalidUsernameException{
        if(userName.matches("[0-9]{3}-[0-9]{4}"))
            return userName;
        else
            throw new InvalidUsernameException();
    }

    public boolean doesUserExist(String userName){
        return userList.containsKey(userName);
    }

    public boolean checkPassword(String userName, String enteredPassword){
        UserData usersData = getUserData(userName);
        return usersData.checkPassword(enteredPassword);
    }
    public TreeMap<String,UserData> getUserMap() {
        return userList;
    }

    public UserData getUserData(String userName) {
        return userList.get(userName);
    }

}
